package com.lkm.market2_0.netapi;


import com.google.gson.Gson;
import com.lkm.frame.P;
import com.lkm.frame.task.StopAble;
import com.lkm.javatool.CollectionHelp;
import com.lkm.javatool.MessageDigestHelp;
import com.lkm.javatool.lang.NameValueP;
import com.lkm.market2_0.ExpandConfig;
import com.lkm.market2_0.entity.ResponEntity;
import com.lkm.market2_0.help.UIP;
import com.lkm.market2_0.m.steelyarddata.OrderEntity;
import com.lkm.market2_0java.help.GsonHelp;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;




public class Api {

    public static String host ;//= "https://dev.check.api.satrace.cn";//http://cs.satrace.cn";

    public static String mehost = "https://zhongzi.web.ic1986.com/api/tc.ashx" ;

    public static void init() {
//        if (Config.instance.isDebug()) {
//            host = "http://zhongzi.web.ic1986.com/api/tc.ashx";//https://dev.balance.api.satrace.cn/";//https://dev.check.api.satrace.cn";

//            host = "http://zhongzi.web.ic1986.com/api/tc.ashx";//https://check.api.satrace.cn";

    }

    public static final String getRes(String act) {

        return host + "/" + act;
    }

    public static final String getAPi(String act) {

        return host ;
    }
    public static final String getAPi(String url,String act) {

        return url ;
    }

    public static final boolean isASTApi(){
        return host != null && host.contains("satrace.cn");
    }
    //    http://note.youdao.com/share/?id=405e8f8df5473170197b3e04701164c0&type=note#/
    public static String init( String deviceMac, StopAble sa) {
        String re = getString( getAPi("balance_data"),
                new NameValueP[]{
                        new NameValueP("act", "balance_data"),
                        new NameValueP("ukey", deviceMac),
                        new NameValueP("mac_address", deviceMac),
                        new NameValueP("do_millisecond", "3000"),
                        new NameValueP("is_do", "3000")
                }, sa);
        return re;

    }

    

    public static String server_breed_price(String steelyardMac, int goodNum, float price, StopAble sa) {
        Map<String, Object> item = new HashMap<>();
        item.put("key", steelyardMac);
        item.put("pul", goodNum);
        item.put("price", price);
        String re = getString(getAPi("server_breed_price"),
                new NameValueP[]{
                        new NameValueP("act", "server_breed_price"),
                        new NameValueP("skey", deviceMac),
                        new NameValueP("json", GsonHelp.toJson(Collections.singletonList(item)))
                }, sa);
        return re;
    }

    

    public static String set_points(String ukey, String oid, String od, String vip, StopAble sa) {

        String re = getString( getAPi("server_set_points"),
                new NameValueP[]{
                        new NameValueP("act", "server_set_points"),
                        new NameValueP("ukey", ukey),
                        new NameValueP("oid", oid),
                        new NameValueP("od", od),
                        new NameValueP("vip", vip)
                }, sa);
        return re;
    }

    

    public static String loadInitInfo( String pushToken, StopAble sa) {

        String re = getString( getAPi("server_get_org"),
                new NameValueP[]{
                        new NameValueP("act", "server_get_org"),
                       
                        new NameValueP("skey", deviceMac),
                        new NameValueP("appkey", pushToken)
                }, sa);
        return re;
    }

    

    public static String upStatus(boolean isme, NameValueP [] statuss, StopAble sa) {
        List<Map<String, String>> datas = new ArrayList<>();

        for (NameValueP s:statuss ) {
            Map<String, String> item = new HashMap<>();
            item.put("ukey", s.getName());

            String  status="0";
            String  version="";
            try {
                String[] statusArr = s.getValue().split("-");
                status=statusArr[0];
                version=statusArr[1];
            }catch (Exception e){

            }
            item.put("status",status );
            item.put("v",version );

            datas.add(item);
        }
        String json = new Gson().toJson(datas);
        if(P.isDebug()) {
            if (UIP.isPrint()) {
                UIP.p("UP上报台秤状态：1正常，3异常，4关闭 " + json,"");
            }
        }

        String url = isme ? getAPi(mehost,"server_balance_cheat") : getAPi( "server_balance_cheat");
        String re = getString( url,
                new NameValueP[]{
                        new NameValueP("act", "server_balance_cheat"),
                        new NameValueP("skey", deviceMac),
                        new NameValueP("json",json)
                }, sa);
        return re;
    }

    

    public static String upOrderBat( String json, StopAble sa) {
        if(P.isDebug()) {
            if(UIP.isPrint()){
                UIP.p("UP上报台秤数据："+json,"");
            }
        }
        String re = getString( getAPi("server_order_add"),
                new NameValueP[]{
                        new NameValueP("act", "server_order_add"),
                        new NameValueP("skey", deviceMac),
                        new NameValueP("json", json)
                }, sa);
        return re;
    }

    

    public static String loadBreedByDeviceMac( String steelyardMac, StopAble sa) {
        List<Map<String, String>> datas = new ArrayList<>();
        Map<String, String> item = new HashMap<>();
        item.put("ukey", steelyardMac);
        datas.add(item);
        String re = getString( getAPi("server_stall_breed"),
                new NameValueP[]{
                        new NameValueP("act", "server_stall_breed"),
                        new NameValueP("skey", deviceMac),
                        new NameValueP("json",new Gson().toJson(datas) )
                }, sa);
        return re;
    }

    

    public static String upBtnUpStatus( String steelyardMac,boolean isOk, StopAble sa) {
        List<Map<String, String>> datas = new ArrayList<>();
        Map<String, String> item = new HashMap<>();
        item.put("ukey", steelyardMac);
        datas.add(item);
        String re = getString( getAPi("server_balance_up"),
                new NameValueP[]{
                        new NameValueP("act", "server_balance_up"),
                        new NameValueP("skey", deviceMac),
                        new NameValueP("json",new Gson().toJson(datas) )
                }, sa);
        return re;
    }



    

    public static String loadSteelyardInfo( String steelyardMac, StopAble sa) {
        List<Map<String, String>> datas = new ArrayList<>();
        Map<String, String> item = new HashMap<>();
        item.put("ukey", steelyardMac);
        datas.add(item);
        String re = getString( getAPi("server_balance_info"),
                new NameValueP[]{
                        new NameValueP("act", "server_balance_info"),
                        new NameValueP("skey", deviceMac),
                        new NameValueP("json",new Gson().toJson(datas) )
                }, sa);
        return re;
    }


    

    public static String checkOkPassword( String steelyardMac,String okPassword, StopAble sa) {
        Map<String, String> item = new HashMap<>();
        item.put("ukey", steelyardMac);
        item.put("code", okPassword);
        String re = getString( getAPi("server_balance_lock"),
                new NameValueP[]{
                        new NameValueP("act", "server_balance_lock"),
                        new NameValueP("skey", deviceMac),
                        new NameValueP("json",new Gson().toJson(item) )
                }, sa);
        return re;
    }


    

    public static String initLoadKey( String deviceMac, StopAble sa) {

        String re = getString( getAPi("load_key"), new NameValueP[]{
                new NameValueP("act", "load_key"),
                new NameValueP("ukey", deviceMac)
        }, sa);
        return re;

    }

    

    public static String getPushlist(  StopAble sa) {
        String re = getString( getAPi("server_get_pushlist"), new NameValueP[]{
                new NameValueP("act", "server_get_pushlist"),
                new NameValueP("skey", deviceMac),
        }, sa);
        return re;

    }

    

    public static String upPushKey( String steelyardMac,String oid,String pushKey, String deviceUncode, StopAble sa) {
        String re = getString( getAPi("server_balance_set"),
                new NameValueP[]{
                        new NameValueP("act", "server_balance_set"),
                        new NameValueP("oid", oid),
                        new NameValueP("ukey",steelyardMac),
                        new NameValueP("deviceUncode", deviceUncode),
                        new NameValueP("appkey", pushKey)
                }, sa);
        return re;
    }

    

    public static String delegatePay(String payurl,String steelyardMac, String oid, OrderEntity orderEntity, String pay_code ,int payType, StopAble sa) {

        String re = getString( getAPi(payurl,"server_get_pay"),
                new NameValueP[]{
                        new NameValueP("act", "server_get_pay"),
                        new NameValueP("oid", oid),
                        new NameValueP("ukey",steelyardMac),
                        new NameValueP("od", orderEntity.orderNum),
                        new NameValueP("pay", ""+payType),
                        new NameValueP("amount", ""+orderEntity.totalPrice),
                        new NameValueP("pay_code",pay_code),
                        new NameValueP("json", ResponEntity.newGson().toJson(Collections.singletonList( orderEntity))),
                }, sa);
        return re;
    }

    public static String delegatePayResult(String payurl,String steelyardMac, String oid, String payOrder, StopAble sa) {
        String re = getString( getAPi(payurl,"server_get_pay_status"),
                new NameValueP[]{
                        new NameValueP("act", "server_get_pay_status"),
                        new NameValueP("oid", oid),
                        new NameValueP("ukey",steelyardMac),
                        new NameValueP("od", payOrder),
                }, sa);
        return re;
    }


    public static String getGoods( String oid, String steelyardMac, StopAble sa) {
        String re = getString( getAPi("server_get_goods"),
                new NameValueP[]{
                        new NameValueP("act", "server_get_goods"),
                        new NameValueP("oid", oid),
                        new NameValueP("ukey",steelyardMac)
                }, sa);
        return re;
    }

    public static String server_get_vip( String oid, String steelyardMac, String vip, StopAble sa) {
        // TODO 需要去掉"https://api.caiding.cn/index.php?s=/api/Zz_scale/index"
        String re = getString(getAPi( "server_get_vip"),
                new NameValueP[]{
                        new NameValueP("act", "server_get_vip"),
                        new NameValueP("oid", oid),
                        new NameValueP("ukey", steelyardMac),
                        new NameValueP("vip", vip),
                }, sa);
        return re;
    }

    public static String getString( String url, NameValueP[] params, StopAble sa) {

        List<NameValueP> listparam = CollectionHelp.toList(params, new ArrayList<NameValueP>());
        String sing = "";
        try {
            sing = mingle(listparam);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        listparam.add(new NameValueP("sign", sing));

        String oid = ExpandConfig.instance.getUIast_oid();
        NameValueP[] heads = new NameValueP[]{
                new NameValueP("oid",oid)
        };

        String re = HttpRequest.getString( url, listparam.toArray(new NameValueP[listparam.size()]),heads, sa);
        return re;
    }

    public static String key = "";
    
    public static String deviceMac = "";


//        Config.instance.saveValue(deviceMac, key);
////        MyApplication.getInstance().getSharedPreferences("privateConfige", Context.MODE_PRIVATE).edit().putString(deviceMac, key).apply();


    public static void initKey(String deviceMac) {
        Api.key = ExpandConfig.instance.loadValue(deviceMac,"");
//        Api.key = MyApplication.getInstance().getSharedPreferences("privateConfige", Context.MODE_PRIVATE).getString(deviceMac, "");
        Api.deviceMac = deviceMac;
    }

    public static String mingle(List<NameValueP> params) throws UnsupportedEncodingException {
        long result = 0;
        for (NameValueP s : params) {
            String md5 = MessageDigestHelp.getDigestHex(s.getValue().getBytes("utf-8"), MessageDigestHelp.MD5);
            long l = Long.parseLong(md5.substring(0, 5), 16);
            result += l;
        }
        String md5 = MessageDigestHelp.getDigestHex(key.getBytes("utf-8"), MessageDigestHelp.MD5);
        long l = Long.parseLong(md5.substring(0, 5), 16);
        result += l;
        return MessageDigestHelp.getDigestHex(String.valueOf(result).getBytes(), MessageDigestHelp.MD5);
    }
}
